<template>
<div>
  <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

  <!-- 卡片区域 -->
  <el-card>
    <el-row>
      <el-col>
        <el-button type="primary" @click="addCateDialog">添加分类</el-button>
      </el-col>
    </el-row>

    <!-- 表格区域 -->
    <tree-table class="treeTable" :data="cateList" :columns="columns" show-index index-text="#" :expand-type="false" :selection-type="false" border :show-row-hover="false">
      <!-- 是否有效 -->
      <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
      </template>
      <!-- 排序 -->
      <template slot="order" slot-scope="scope">
        <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
        <el-tag size="mini" type="warning" v-else-if="scope.row.cat_level === 1">二级</el-tag>
        <el-tag size="mini" type="danger" v-else>三级</el-tag>
      </template>
      <!-- 操作 -->
      <template slot="opt">
        <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
        <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
      </template>
    </tree-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="quaryInfo.pagenum"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="quaryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>

  <!-- 添加分类的对话框 -->
  <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="cateDialogVisibleClose">
    <el-form :model="addCateruleForm" :rules="addCaterules" ref="addCateruleFormRef" label-width="100px">
      <el-form-item label="分类名称: " prop="cat_name">
      <el-input v-model="addCateruleForm.cat_name"></el-input>
      </el-form-item>
      <el-form-item label="父级名称: ">
        <el-cascader v-model="selectedKeys" :options="parentsCateList" :props="cascaderProps" @change="cascaderChanged" clearable></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  data() {
    return {
      // 分类参数
      quaryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 拿到商品分类的数据列表
      cateList: [],
      // 商品分类数据的总数
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateDialogVisible: false,
      // 对话框的绑定属性
      addCateruleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 对话框校验规则
      addCaterules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentsCateList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedKeys: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.quaryInfo
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类列表数据失败!')
      }
      console.log(res.data)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 分页的事件
    handleSizeChange(newSize) {
      this.quaryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage) {
      this.quaryInfo.pagenum = newPage
      this.getCateList()
    },
    // 添加分类对话框的点击事件
    addCateDialog() {
      // 点击打开对话框前先获取父级分类数据
      this.getparentsList()
      this.addCateDialogVisible = true
    },
    // 获取父级分类数据的事件
    async getparentsList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级名称数据失败!')
      }
      this.parentsCateList = res.data
    },
    // 级联列表的监听变化事件
    cascaderChanged() {
      // 如果是选择不是一级分类则selectedKeys数组中的长度大于0,
      if (this.selectedKeys > 0) {
        this.addCateruleForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateruleForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateruleForm.cat_pid = 0
        this.addCateruleForm.cat_level = 0
      }
    },
    // 添加分类对话框关闭事件
    cateDialogVisibleClose() {
      this.selectedKeys = []
      this.$refs.addCateruleFormRef.resetFields()
      this.addCateruleForm.cat_pid = 0
      this.addCateruleForm.cat_level = 0
    },
    // 提交表单数据
    addCate() {
      console.log(this.addCateruleForm)
      this.$refs.addCateruleFormRef.validate(async valide => {
        if (!valide) return
        const { data: res } = await this.$http.post('categories', this.addCateruleForm)

        if (res.meta.status !== 201) {
          return this.$message.error('提交表单数据失败!')
        }
        this.$message.success('提交表单数据成功!')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 20px;
}
.el-cascader {
  width: 100%;
}
</style>
